<?php
class Admin_AccountController extends Zend_Controller_Action{

	public function init(){
		parent::init();

	}

	protected function buildTableAccounts() {
		$tableHtml = "<table cellpadding = '0' cellspacing = '0' class = 'table-data' border='0'>
						 <thead>
						  <tr>
						    <th>Full name</th>
						    <th>User name</th>
						    <th>Password</th>
						    <th>Address</th>
						    <th>Phone</th>
						    <th>Email</th>
						    <th>Role</th>
						    <th>Update</th>
						    <th>Delete</th>
						  </tr>
						  </thead>
					  ";
		$userMapper = new Front_Model_UserMapper();
		$entries = $userMapper->fetchAll();

		if (isset($entries)) {
			$url = new Zend_View_Helper_Url();
			foreach ($entries as $row) {
				$rowStr = '<tr>';
				//fullname
				$fullname = $row->_fullname;
				if (!isset($fullname)) {
					$fullname = '';
				}
				$rowStr = $rowStr .'<td>'.$fullname.'</td>';
				// username
				$username = $row->_username;
				if (!isset($username)) {
					$username = '';
				}
				$rowStr = $rowStr .'<td>'.$username.'</td>';
				//$password
				$password = $row->_password;
				if (!isset($password)) {
					$password = '';
				}
				$rowStr = $rowStr .'<td>'.$password.'</td>';
				//$address
				$address = $row->_address;
				if (!isset($address)) {
					$address = '';
				}
				$rowStr = $rowStr .'<td>'.$address.'</td>';
				//$phone
				$phone = $row->_phone;
				if (!isset($phone)) {
					$phone = '';
				}
				$rowStr = $rowStr .'<td>'.$phone.'</td>';
				//$email
				$email = $row->_email;
				if (!isset($email)) {
					$email = '';
				}
				$rowStr = $rowStr .'<td>'.$email.'</td>';

				// $role
				$role = $row->_role;
				if (isset($role)) {
					if ($role == 1) {
						$role  = 'Admin';
					} else if ($role == 2){
						$role = 'User';
					}
				}
				if (!isset($role)) {
					$role = '';
				}
				$rowStr = $rowStr .'<td>'.$role.'</td>';

				//update link
				$updatelink = $url->url(array('module'=>'admin', 'controller'=>'account','action'=>'updateaccount','id'=>$row->_uid));
				if (!isset($updatelink)) {
					$updatelink = '';
				}
				$rowStr = $rowStr ."<td><a href = '".$updatelink."'>Update</a>".'</td>';
				// delete link
				$deletelink = $url->url(array('module'=>'admin', 'controller'=>'account','action'=>'deleteaccount','id'=>$row->_uid));
				if (!isset($deletelink)) {
					$deletelink = '';
				}
				$rowStr = $rowStr ."<td><a href = '".$deletelink."'>Delete</a>".'</td>';
				$rowStr = $rowStr.'</tr>';
				$tableHtml = $tableHtml.$rowStr;
			}
		}

		$tableHtml = $tableHtml .'</table>';

		$this->view->accounts = $tableHtml;
	}

	public function indexAction(){
		$date  = new Zend_Date();
		$this->view->currentDateLabel = $date->toString();
		$this->_forward('addaccount','account','admin');
	}


	public function addaccountAction(){
		//$formAccount   = new Admin_Form_Account('create');
		$this->buildTableAccounts();
		$form = new Admin_Form_Account('add');
		$form->setMethod('post');
		$this->view->form = $form;

		//new user info
		$userinfo = new Front_Model_User();

		if ($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData))
			{
				$userinfo->_fullname = $form->getValue('fullname');
				$userinfo->_username = $form->getValue('username');
				$userinfo->_password = $form->getValue('password');
				$userinfo->_address = $form->getValue('address');
				$userinfo->_phone = $form->getValue('phone');
				$userinfo->_email = $form->getValue('email');
				$userinfo->_role = $form->getValue('role');
				$userinfo->_remain = 0;
				$user = new Front_Model_UserMapper();
				$user->save($userinfo);
				My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'account', 'index');
				//$this->_helper->redirector('index');
			}
		}
	}

	public function updateaccountAction() {
		//$form   = new Admin_Form_Account('update');
		$this->buildTableAccounts();
		$uid = $this->getRequest()->getParam('id');
		$userMapper = new Front_Model_UserMapper();
		$userinfo = new Front_Model_User();
		$hasAccount = $userMapper->find($uid, $userinfo);
		if (isset($hasAccount) && isset($userinfo))
		{
			// Create form
			$formAccount  = new Admin_Form_Account('update');
			$formAccount->setMethod('post');
			$formAccount->populateData($userinfo);

			// Bind data
			$this->view->form = $formAccount;

			if ($this->getRequest()->isPost())
			{
				$formData = $this->getRequest()->getPost();
				if ($formAccount->isValid($formData)) {
					$fullname = $this->getRequest()->getParam('fullname');
					$username = $this->getRequest()->getParam('username');
					$password = $this->getRequest()->getParam('password');
					$address = $this->getRequest()->getParam('address');
					$phone = $this->getRequest()->getParam('phone');
					$email = $this->getRequest()->getParam('email');
					$role = $this->getRequest()->getParam('role');
					$remain = $this->getRequest()->getParam('remain');

					$userinfo->_uid = $uid;
					$userinfo->_fullname = $fullname;
					$userinfo->_username = $username;
					$userinfo->_password = $password;
					$userinfo->_address = $address;
					$userinfo->_phone = $phone;
					$userinfo->_email = $email;
					$userinfo->_role = $role;
					$userinfo->_remain = $remain;
					$userMapper = new Front_Model_UserMapper();
					$userMapper->save($userinfo);
					My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'account', 'index');
				}

			}
		}
	}

	public function deleteaccountAction() {
		$uid = $this->getRequest()->getParam('id');
		$userMapper = new Front_Model_UserMapper();
		if (isset($uid)) {
			$userMapper->delete($uid);
			My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'account', 'index');
		}
	}


}